<template>
  <div>
    <el-dialog :title="dialogPassword.title" :visible.sync="dialogPassword.show" width="40%" @open="openFun">
      <el-form ref="form" :model="formData" label-width="80px" :rules="rules">
          <el-form-item label="登录密码" prop="password" >
            <el-input show-password placeholder="登录密码" v-model="formData.password"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword" >
            <el-input show-password placeholder="确认密码" v-model="formData.confirmPassword"></el-input>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="handleSubmit('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    dialogPassword: Object,
    formData: Object
  },
  data() {
    var checkPassword = (rule, value, callback) => {
      if (!value) {
          return callback(new Error('确认密码不能为空'))
      }
      if (this.formData.password !== value) {
          callback(new Error('2次密码不相等'))
      } else {
          callback()
      }
    }
    return {
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{6,30}$/, message: '密码为数字，小写字母，大写字母，特殊符号 至少包含三种，长度为 8 - 30位，' }
          ],
        confirmPassword: [{ required: true, validator: checkPassword, trigger: 'blur' }],
      }
    }
  },
  methods: {
    // 弹窗打开触发的方法
    openFun() {

    },

    // 点击确定按钮触发
    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("handlePasswordSubmit")
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 点击取消按钮触发
    resetForm(formName) {
      this.dialogPassword.show = false
      this.$refs[formName].resetFields();
    }
  },
}
</script>

<style lang="scss" scoped>
</style>